<?php

namespace app\common\validate;

use think\Validate;

class Behavior extends Validate
{

    /**
     * 正则
     */
    protected $regex = ['format' => '[a-z0-9_\/]+'];

    /**
     * 验证规则
     */
    protected $rule = [
        'category_id' => 'require',
        'name' => 'require|unique:Behavior,name^school_id^category_id^type',
        'type' => 'require',
        'score' => 'require|positiveFloat',
    ];

    /**
     * 提示消息
     */
    protected $message = [
        //'score.positiveFloat' => '分值 必须为正数'
    ];

    /**
     * 字段描述
     */
    protected $field = [
    ];

    /**
     * 验证场景
     */
    protected $scene = [
        'add' => ['category_id', 'name', 'type', 'score'],
        'edit' => ['category_id', 'name' => 'require|unique:Behavior,name^school_id^category_id^type^id', 'type', 'score'],
    ];

    public function __construct(array $rules = [], $message = [], $field = [])
    {
        $this->field = [
            'category_id' => __('行为分类'),
            'name' => __('名称'),
        ];
        parent::__construct($rules, $message, $field);
    }

    protected function positiveFloat($value)
    {
        if (is_numeric($value) && $value > 0) {
            return true;
        } else {
            return '分值 必须为正数';
        }
    }


}
